package com.metaera.app.controller.view;

import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.HomeNewsTypeResponse;
import com.metaera.app.ui.adapters.SimpleFragmentStatePagerAdapter;
import com.metaera.app.ui.adapters.navigator.FlashNewsNavigatorAdapter;
import com.metaera.app.ui.fragments.FlashNewsItemFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

public class FlashNewsCategoryView implements INetworkRequestView<List<HomeNewsTypeResponse>> {

    private final ViewPager       mViewPager;
    private final MagicIndicator  mMagicIndicator;
    private final FragmentManager mFragmentManager;

    public FlashNewsCategoryView(FragmentManager fm, ViewPager viewPager, MagicIndicator magicIndicator) {
        mFragmentManager = fm;
        mViewPager = viewPager;
        mMagicIndicator = magicIndicator;
    }

    @Override
    public void successful(List<HomeNewsTypeResponse> result) {
        List<String> titleList = new ArrayList<>();
        List<Fragment> fragmentList = new ArrayList<>();

        for (HomeNewsTypeResponse response : result) {
            fragmentList.add(FlashNewsItemFragment.newInstance(response.getId()));
            titleList.add(response.getName());
        }

        CommonNavigator commonNavigator = new CommonNavigator(mViewPager.getContext());
        commonNavigator.setAdapter(new FlashNewsNavigatorAdapter(mViewPager, titleList));

        // 导航器设置适配器
        mMagicIndicator.setNavigator(commonNavigator);
        mViewPager.setAdapter(new SimpleFragmentStatePagerAdapter(mFragmentManager, fragmentList));

        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    public void loading() {

    }

    @Override
    public void failure(Throwable throwable) {

    }

}